package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootLottery;
import org.footballmanager.pandafootball.system.domain.FootTeam;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.JsonReturn;
import org.footballmanager.pandafootball.system.form.impl.FootLotterySearchForm;
import org.footballmanager.pandafootball.system.mapper.FootLotteryMapper;
import org.footballmanager.pandafootball.system.service.FootChipService;
import org.footballmanager.pandafootball.system.service.FootLotteryService;
import org.footballmanager.pandafootball.system.service.FootTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FootLotteryServiceImpl extends ServiceImpl<FootLottery> implements FootLotteryService {
    /*白色	75以下	5分钟	10PB
        绿色	75-79	30分钟	30PB
        蓝色	80-83	2小时	50PB
        紫色	84-89	6小时	80PB
        橙色	90-100	24小时	100PB */
    //都换算成秒
    private static final long WHITE = 300;
    private static final long GREEN = 1800;
    private static final long BLUE = 7200;
    private static final long PERPLE = 21600;
    private static final long ORANGE = 86400;

    private static final double WHITEPRICE = 10D;
    private static final double GREENPRICE = 30D;
    private static final double BLUEPRICE = 50D;
    private static final double PURPLEPRICE = 80D;
    private static final double ORANGEPRICE = 100D;

    @Autowired
    private FootLotteryMapper footLotteryMapper;
    @Autowired
    private FootChipService footChipService;
    @Autowired
    private FootTeamService footTeamService;

    /**
     * 重新计算白人碎片抽取时间
     *
     * @param userId  用户主键
     * @param isPay   是否付款
     * @param request 请求
     * @return json返回数据
     */
    @Override
    public JsonReturn reComputeWhiteDate(Long userId, boolean isPay, HttpServletRequest request) {
        return processRandomChip(userId, isPay, request, WHITEPRICE, WHITE, 1, "白色");
    }

    /**
     * 重新计算普通球星碎片抽取时间
     *
     * @param userId  用户主键
     * @param isPay   是否付款
     * @param request 请求
     * @return json返回数据
     */
    @Override
    public JsonReturn reComputeGreenDate(Long userId, boolean isPay, HttpServletRequest request) {
        return processRandomChip(userId, isPay, request, GREENPRICE, GREEN, 2, "绿色");
    }

    /**
     * 重新计算洲际球星碎片抽取时间
     *
     * @param userId  用户主键
     * @param isPay   是否付款
     * @param request 请求
     * @return json返回数据
     */
    @Override
    public JsonReturn reComputeBlueDate(Long userId, boolean isPay, HttpServletRequest request) {
        return processRandomChip(userId, isPay, request, BLUEPRICE, BLUE, 3, "蓝色");
    }

    /**
     * 重新计算超级球星碎片抽取时间
     *
     * @param userId  用户主键
     * @param isPay   是否付款
     * @param request 请求
     * @return json返回数据
     */
    @Override
    public JsonReturn reComputePurpleDate(Long userId, boolean isPay, HttpServletRequest request) {
        return processRandomChip(userId, isPay, request, PURPLEPRICE, PERPLE, 4, "紫色");
    }

    /**
     * 重新计算顶级球星碎片抽取时间
     *
     * @param userId  用户主键
     * @param isPay   是否付款
     * @param request 请求
     * @return json返回数据
     */
    @Override
    public JsonReturn reComputeOrangeDate(Long userId, boolean isPay, HttpServletRequest request) {
        return processRandomChip(userId, isPay, request, ORANGEPRICE, ORANGE, 5, "橙色");
    }

    /**
     * 处理随机碎片数量
     *
     * @param userId    用户主键
     * @param isPay     是否付款
     * @param request   请求
     * @param price     价格
     * @param initPrice 初始价格
     * @param type      类型
     * @param color     颜色
     * @return json返回数据
     */
    private JsonReturn processRandomChip(Long userId, boolean isPay, HttpServletRequest request, Double price,
                                         Long initPrice, Integer type, String color) {
        if (isPay) {
            FootTeam team = footTeamService.getTeamByUserid(userId);
            if (team != null && team.getQdZzj() > price) {
                team.setQdZzj(team.getQdZzj() - price);
                team.setYy("因购买" + color + "碎片花费：" + new DecimalFormat(",###").format(price) +
                        "PB,当前的球队总资金为:" + new DecimalFormat(",###").format(team.getQdZzj()) + "PB");
                footTeamService.updateAll(team, request);
            } else {
                return new JsonReturn("小子！这里是有钱人来的地方");
            }
        }else{
            FootLottery lottery = findLotteryByUserId(userId);
            if (null != lottery) {
                Date date = new Date(new Date().getTime() + initPrice * 1000);
                if (type.equals(1)) {
                    lottery.setLotteryDateWhite(date);
                } else if (type.equals(2)) {
                    lottery.setLotteryDateGreen(date);
                } else if (type.equals(3)) {
                    lottery.setLotteryDateBlue(date);
                } else if (type.equals(4)) {
                    lottery.setLotteryDatePurple(date);
                } else {
                    lottery.setLotteryDateOrange(date);
                }
                this.updateNotNull(lottery, request);
            }
        }
        footChipService.randomGetChip(userId, type, 1, request);
        return new JsonReturn(Constants.OK);
    }

    /**
     * 根据用户主键查找碎片时间数据
     *
     * @param userId 用户主键
     * @return 碎片时间数据
     */
    @Override
    public FootLottery findLotteryByUserId(Long userId) {
        Example example = new Example(FootLottery.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(Constants.USERID, userId);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 新建用户的碎片时间信息
     *
     * @param user    用户
     * @param request 请求
     */
    @Override
    public void saveNewLottery(FootUser user, HttpServletRequest request) {
        FootLottery lo = new FootLottery();
        Date date = new Date();
        lo.setUserId(user.getId());
        lo.setLotteryDateWhite(date);
        lo.setLotteryDateGreen(date);
        lo.setLotteryDateBlue(date);
        lo.setLotteryDatePurple(date);
        lo.setLotteryDateOrange(date);
        this.save(lo, request);
    }

    /**
     * 翻页方法
     *
     * @param footLotterySearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootLottery> findall(FootLotterySearchForm footLotterySearchForm) {
        return footLotteryMapper.findall(footLotterySearchForm);
    }

    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootLottery get(Serializable id) {
        return footLotteryMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname            实体类名
     * @param map                   参数
     * @param footLotterySearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootLotterySearchForm footLotterySearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footLotterySearchForm);
        Page<FootLottery> page = PageHelper.startPage(footLotterySearchForm.getPageIndex(), footLotterySearchForm.getPageSize());
        List<FootLottery> list = this.findall(footLotterySearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 根据用户主键删除对应碎片时间记录
     *
     * @param userId  用户主键
     * @param request 请求
     */
    @Override
    public void deleteByUserId(Long userId, HttpServletRequest request) {
        FootLotterySearchForm footLotterySearchForm = new FootLotterySearchForm();
        footLotterySearchForm.setUserId(userId);
        List<FootLottery> list = this.findall(footLotterySearchForm);
        if (list != null && !list.isEmpty()) {
            for (FootLottery footLottery : list) {
                this.delete(footLottery.getId(), request);
            }
        }
    }
}
